package org.ishequ.service;

import java.util.ArrayList;
import java.util.List;

import org.ishequ.domain.Person;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PersonService {
	
	private PersonOpenHelper personOpenHelper;
	
	public PersonService(Context context){
		personOpenHelper = new PersonOpenHelper(context);
	}
	
	public void payment(int fromid,int toid,float amount){
		SQLiteDatabase db = personOpenHelper.getWritableDatabase();
		db.beginTransaction();
		try{
			db.execSQL("update person set amount = amount + ? where personid=?",
					new String[]{String.valueOf(amount),String.valueOf(toid)});
			db.execSQL("update person set amount = amount - ? where personid=?",
					new String[]{String.valueOf(amount),String.valueOf(fromid)});
			db.setTransactionSuccessful();
		}finally{
			db.endTransaction();
		}
		
	}
	
	public void save(Person person){
		SQLiteDatabase db = personOpenHelper.getWritableDatabase();
		db.execSQL("insert into person(name,phone,amount) values(?,?,?)",
				new String[]{person.getName(),person.getPhone(),
				String.valueOf(person.getAmount())});
	}
	
	public void delete(Integer id){
		SQLiteDatabase db = personOpenHelper.getWritableDatabase();
		db.execSQL("delete from person where personid=?",new String[]{id.toString()});
	}

	public void update(Person person){
		SQLiteDatabase db = personOpenHelper.getWritableDatabase();
		db.execSQL("update person set name = ?,phone=? where personid=?",new String[]{person.getName(),person.getPhone(),String.valueOf(person.getId())});
	}
	
	public Person find(Integer id){
		SQLiteDatabase db = personOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from person where personid=?",new String[]{id.toString()});
		if(cursor.moveToFirst()){
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String phone = cursor.getString(cursor.getColumnIndex("phone"));
			int personid = cursor.getInt(cursor.getColumnIndex("personid"));
			Person person = new Person(personid,name,phone);
			return person;
		}
		cursor.close();
		return null;
	}
	
	public List<Person> getScrollData(int offset,int maxResult){
		List<Person> persons = new ArrayList<Person>();
		SQLiteDatabase db = personOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from person limit ?,?",new String[]{String.valueOf(offset),String.valueOf(maxResult)});
		while(cursor.moveToNext()){
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String phone = cursor.getString(cursor.getColumnIndex("phone"));
			int personid = cursor.getInt(cursor.getColumnIndex("personid"));
			float amount = cursor.getFloat(cursor.getColumnIndex("amount"));
			persons.add(new Person(personid,name,phone,amount));
		}
		cursor.close();
		return persons;
	}
	
	public Cursor getCursorScrollData(int offset,int maxResult){//注意 要有"_id"字段
		SQLiteDatabase db = personOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select *,personid as _id from person limit ?,?",new String[]{String.valueOf(offset),String.valueOf(maxResult)});
		return cursor;
	}

	public long getCount(){
		SQLiteDatabase db = personOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select count(*) from person",null);
		cursor.moveToFirst();
		long result = cursor.getLong(0);
		cursor.close();
		return result;
	}
}
